home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / dlghlp.exe / DLGHELP.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-24  |  4.9 KB  |  133 lines

  1. Unit DlgHelp;
  2.  
  3. { Using a message filter to create context sensitive help in a dialog.
  4.  
  5.   Now it's possible to use chained dialogs, each with his own F1 notification
  6.   message. The notification message is send with a wm_Command and a given 
  7.   Ctl_ID, so you can use one routine to answer the help button and the F1 key. 
  8.   Use an object procedure with ...virtual id_First + Ctl_ID!
  9.  
  10.   Only the last installed filter routine is called. So you must use modal 
  11.   dialogs!
  12.  
  13.   Compiler: BP7 with OWL
  14.  
  15.   Written: 7/18/91       By: Pat Ritchey CIS ID: [70007,4660]
  16.   Changed: 5/18/93       By: Michael Denzlein - S.I.P. CIS ID: [100120,2601] 
  17. }
  18.  
  19. {*************************************************************************}
  20. {*****************************} Interface {*******************************}
  21. {*************************************************************************}
  22.  
  23. uses WinTypes,WinProcs,OWindows,ODialogs,Objects;
  24.  
  25. {*************************************************************************}
  26.  
  27. Procedure InstallFilter(AWnd : hWnd; IDNr : Word);
  28.     { Installs filter procedure for window 'AWnd' with the notification message
  29.       wm_Command with 'MsgNr' in wParam. It's possible to chain the filters! }
  30.  
  31. Procedure RemoveFilter;
  32.     { Removes one filter procedure from the list. }
  33.  
  34. Procedure DisableHelp;
  35. Procedure EnableHelp;
  36.     { while WINHELP is called, other help keys should be ignored, so use DisableHelp at
  37.       the beginning of your help routine and EnableHelp at the end. }
  38.  
  39. {*************************************************************************}
  40. {***************************} Implementation {****************************}
  41. {*************************************************************************}
  42.  
  43. Type  PFilterRec = ^TFilterRec;
  44.         TFilterRec =    record
  45.                                 HelpMsgID : Word;    { ID to send with the wm_Command notification }
  46.                          HelpParent: hWnd;    { Parent who receives this message }
  47.                             end;
  48. var
  49.       { Global variables used by MsgFilter functions }
  50.       MsgInstance,                            { Holds the ProcInstance of MsgFilter }
  51.     OldHook                : TFarProc;        { The previous hook function }
  52.    HlpFilterColl        : PCollection;    { Collection to save the message receiver and the ID-Nr }
  53.  
  54. const
  55.     HlpCollExists        : Boolean = False;    { Used to handle the HlpFilterColl on the fly }
  56.       DisableHelpFlag    : boolean = False;    { Used to temporary disable the F1 functionality }
  57.  
  58. Function MsgFilter(nCode : integer; wParam : word; var Msg : TMsg) : longint; export;
  59. begin
  60.     MsgFilter := 0;
  61.     if nCode<0 then begin
  62.        MsgFilter := DefHookProc(nCode,wParam,Longint(@Msg),@OldHook);
  63.        exit;
  64.    end;
  65.     { if a help dialog box is already displayed don't display another }
  66.     if DisableHelpFlag then
  67.         exit;
  68.     { if this is not a dialog box message then ignore it }
  69.     if nCode<>MSGF_DialogBox then
  70.         exit;
  71.     { Ignore all but KEYDOWN messages }
  72.     if Msg.message<>WM_KEYDOWN then
  73.         exit;
  74.     { Ignore all keys except F1 }
  75.     if Msg.wParam<>VK_F1 then
  76.         exit;
  77.     { at this point we know that the message is an F1 keypress in a
  78.       dialog box.  Set the return value to signify that *WE* have handled
  79.       the message and then send a message to the window that will process
  80.       the help message, passing the window handle of the control with focus
  81.       in lParamLo }
  82.     MsgFilter := 1;
  83.    with TFILTERREC(HlpFilterColl^.At(HlpFilterColl^.Count-1)^) do
  84.         SendMessage(HelpParent, wm_Command, HelpMsgID, Msg.hWnd);
  85. end;
  86.  
  87. Procedure InstallFilter(AWnd : hWnd; IDNr : Word);
  88. var    Puf : PFilterRec;
  89. begin
  90.    if not HlpCollExists then begin                                { if there is no collection yet }
  91.         HlpFilterColl := new(PCollection, init(1,1));                { create one }
  92.       HlpCollExists := True;
  93.        MsgInstance := MakeProcInstance(@MsgFilter,hInstance);    { and the filter procedure }
  94.        OldHook := SetWindowsHook(WH_MSGFilter,MsgInstance);
  95.        DisableHelpFlag := false;
  96.    end;
  97.    Puf := New(PFilterRec);                                            { add the data to the collection }
  98.    Puf^.HelpParent := AWnd;
  99.    Puf^.HelpMsgID := IDNr;
  100.    HlpFilterColl^.Insert(Puf);
  101. end;
  102.  
  103. Procedure RemoveFilter;
  104. begin
  105.       if HlpCollExists then begin                                    { if there is a collection continue }
  106.        HlpFilterColl^.AtDelete(HlpFilterColl^.Count-1);    { delete the last entry }
  107.       if HlpFilterColl^.Count=0 then begin                    { if it was the very last }
  108.            UnhookWindowsHook(WH_MSGFILTER, MsgInstance);        { kill filter procedure }
  109.              FreeProcInstance(MsgInstance);
  110.              dispose(HlpFilterColl, Done);                                { and kill the collection }
  111.          HlpCollExists := False;
  112.       end;
  113.       end;
  114. end;
  115.  
  116. Procedure DisableHelp;
  117. begin
  118.     DisableHelpFlag := True;
  119. end;
  120.  
  121. Procedure EnableHelp;
  122. begin
  123.     DisableHelpFlag := False;
  124. end;
  125.  
  126. {*************************************************************************}
  127. {*************************************************************************}
  128. {*************************************************************************}
  129.  
  130. begin
  131.     HlpFilterColl := nil;
  132. end.
  133.